﻿import random
from locust import HttpUser, task, between
from locust import events
import time
import requests
headers = {'Content-Type': 'application/json;charset=utf-8'}
# from locust.clients import HttpSession

# token = '11c33373fa25ef097a878d4658cf8533'

# def get_token():
#     try:
#         # 设置sesssion
#         global session
#         session = HttpSession("http://www.etude.cn",)
#         url = 'http://www.etude.cn/app/oauth/token.json?appId=100000&secret=ab0427a6b2ef96ce3196bc&code=616143'
#         # headers = {"content-type": "application/x-www-form-urlencoded"}
#         response = requests.get(url=url, headers=headers)
#         json_resp = response.json()
#         # print(json_resp["data"])
#         token1 = json_resp["data"]["token"]
#         # print(token)
#     except Exception as e:
#         raise Exception("获取token失败请检查!!!")
#     return token1
# token = get_token()

def my_assert(response,my_dict):
    """
    :param my_dict:  例{"code": 2020，success：false}
    :return:
    """
    try:
        if response.status_code != 200:
            response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))
        else:
            json_resp = response.json()
            for k in my_dict.keys():
                if json_resp[k] != my_dict[k]:
                    response.failure("返回值【%s】为【%s】与预期值【%s】不符，返回报文【%s】" % (k,json_resp[k],my_dict[k], response.text))
    except Exception as e:
        response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))

@events.test_start.add_listener
def on_test_start(**kwargs):

    print("A new test is starting")

@events.test_stop.add_listener
def on_test_stop(**kwargs):
    print("A new test is ending")

class Test1(HttpUser):

    weight = 1
    wait_time = between(0.5, 0.6)
    @task
    def index_page(self):
        # # 首页
        # ran_num = random.randint(100000, 999999)
        # with self.client.get("/app/oauth/token.json?appId=100000&secret=ab0427a6b2ef96ce3196bc&code=666666", headers=headers, catch_response=True,timeout=60) as response:
        #     my_assert(response,{"code":1000,"success": True})
        #     try:
        #         json_resp = response.json()
        #         token = json_resp["data"]["token"]
        #     except Exception as e:
        #         response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))

        my_headers = {"x-et-token": self.token}

        # #导航栏
        data = {"stype":"middle"}
        with self.client.post("/app/nav/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response1:
            my_assert(response1,{"code":1000,"success": True})

        #本周热销
        data = {"searchBestClassCode": "WA"}
        with self.client.post("/app/product/best/data.json",json=data,  headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response,{"code":1000,"success": True})

        #首页图
        data = {"bnrTypeCd":"BL14"}
        with self.client.post("/app/banner/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response,{"code":1000,"success": True})

        #客户信息
        with self.client.post("/app/customer/info.json", headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response,{"code":2020,"success": False})

        #热门话题
        data = {"type":"W"}
        with self.client.post("/app/hotissue/data.json",json=data,  headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response,{"code":1000,"success": True})

        # 关键词
        with self.client.post("/app/keyword/data.json", headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response, {"code": 1000, "success": True})

        # 首页图2
        data = {"bnrTypeCd": "BL01"}
        with self.client.post("/app/banner/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response, {"code": 1000, "success": True})

        # 当季新品
        data = {"listScale": 8,"pageNo":1,"searchBestClassCode":"NE"}
        with self.client.post("/app/product/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response, {"code": 1000, "success": True})

        # 首页图3
        data = {"bnrTypeCd": "BL04"}
        with self.client.post("/app/banner/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response, {"code": 1000, "success": True})

        # 购物车列表数据
        with self.client.post("/app/cart/listdata.json", headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response, {"code": 2020, "success": False})

        # 客户信息2
        with self.client.post("/app/customer/info.json", headers=my_headers, catch_response=True,timeout=60) as response:
            my_assert(response, {"code": 2020, "success": False})

    def get_token(self):
        try:
            # 设置sesssion
            # global session
            # session = HttpSession("http://www.etude.cn",)
            url = 'http://www.etude.cn/app/oauth/token.json?appId=100000&secret=ab0427a6b2ef96ce3196bc&code=616143'
            # headers = {"content-type": "application/x-www-form-urlencoded"}
            response = self.client.get(url=url, headers=headers)
            json_resp = response.json()
            token1 = json_resp["data"]["token"]
        except Exception as e:
            raise Exception("获取token失败请检查!!!")
        return token1


    def on_start(self):
        self.token = self.get_token()
        # print(self.token)
        # pass




# class Test2(HttpUser):
#     weight = 1
#     wait_time = between(0.5, 0.6)
#     # 商品列表查询页面
#     @task
#     def productlist_page(self):
#         # global tokenB
#         # if tokenB=="":
#         # ran_num = random.randint(100000, 999999)
#         with self.client.get("/app/oauth/token.json?appId=100000&secret=ab0427a6b2ef96ce3196bc&code=666666", headers=headers, catch_response=True,timeout=60) as response:
#             my_assert(response,{"code":1000,"success": True})
#             try:
#                 json_resp = response.json()
#                 tokenB = json_resp["data"]["token"]
#             except Exception as e:
#                 response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))
#
#         my_headers = {"x-et-token": tokenB}
#
#         # 客户信息
#         with self.client.post("/app/customer/info.json", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 2020, "success": False})
#
#         # 热门话题
#         data = {"type": "W"}
#         with self.client.post("/app/hotissue/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         # 关键词
#         with self.client.post("/app/keyword/data.json", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         #导航栏
#         data = {"stype":"middle"}
#         with self.client.post("/app/nav/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response,{"code":1000,"success": True})
#
#         #本周热销
#         data = {"searchBestClassCode": "WA"}
#         with self.client.post("/app/product/best/data.json",json=data,  headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response,{"code":1000,"success": True})
#
#         # 首页图
#         data = {"bnrTypeCd": "BL14"}
#         with self.client.post("/app/banner/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         # 首页图
#         data = {"bnrTypeCd": "BL13"}
#         with self.client.post("/app/banner/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         # 商品全量查询
#         data = {"listScale": 1,"pageNo":2}
#         with self.client.post("/app/product/search.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         # 购物车列表数据
#         with self.client.post("/app/cart/listdata.json", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 2020, "success": False})
#
#         # 客户信息
#         with self.client.post("/app/customer/info.json", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 2020, "success": False})
#
#         # 商品条件查询
#         data = {"listScale": 1,"pageNo":2,"searchCategoryCode":"100200%2C100201%2C100202%2C100203%2C100204%2C100205%2C100206%2C100207%2C100208"}
#         with self.client.post("/app/product/search.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#     def on_start(self):
#         pass
#
# class Test3(HttpUser):
#
#     weight = 1
#     wait_time = between(0.5, 0.6)
#     # 商品详情页面
#     @task
#     def productlist_page(self):
#         # global tokenC
#         # if tokenC=="":
#         # ran_num = random.randint(100000, 999999)
#         with self.client.get("/app/oauth/token.json?appId=100000&secret=ab0427a6b2ef96ce3196bc&code=666666", headers=headers, catch_response=True,timeout=60) as response:
#             my_assert(response,{"code":1000,"success": True})
#             try:
#                 json_resp = response.json()
#                 tokenC = json_resp["data"]["token"]
#             except Exception as e:
#                 response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))
#
#         my_headers = {"x-et-token": tokenC}
#         # 客户信息
#         with self.client.post("/app/customer/info.json", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 2020, "success": False})
#
#         # 热门话题
#         data = {"type": "W"}
#         with self.client.post("/app/hotissue/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         # 关键词
#         with self.client.post("/app/keyword/data.json",  headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         #导航栏
#         data = {"stype":"middle"}
#         with self.client.post("/app/nav/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response,{"code":1000,"success": True})
#
#         #本周热销
#         data = {"searchBestClassCode": "WA"}
#         with self.client.post("/app/product/best/data.json",json=data,  headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response,{"code":1000,"success": True})
#
#         # 首页图
#         data = {"bnrTypeCd": "BL14"}
#         with self.client.post("/app/banner/data.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         #商品详情
#         with self.client.post("/app/product/detail.json?prdCode=100020444", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         #商品浏览
#         with self.client.post("/app/product/browse.json?prdseq=100020444", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         data = {"seq": "100020444"}
#         with self.client.post("/app/product/hot/color.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         data = {"productSeq": "100020444","listScale": 20,"pageNo":1}
#         with self.client.post("/app/review/purchasereview/list.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         #商品活动信息
#         with self.client.post("/app/product/event/list.json?prdCode=100020444", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         #商品赠品信息
#         data = {"prdCode": "100020444"}
#         with self.client.post("/app/product/gift/list.json?prdCode=100020444", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 1000, "success": True})
#
#         #本周热销
#         data = {"searchBestClassCode": "WA","searchBestRankingLimit":10}
#         with self.client.post("/app/product/best/data.json",json=data,  headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response,{"code":1000,"success": True})
#
#
#         data = {"listScale": 10,"pageNo":1}
#         with self.client.post("/app/review/photoreview/list.json",json=data,  headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response,{"code":1000,"success": True})
#
#         # 购物车列表数据
#         with self.client.post("/app/cart/listdata.json", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 2020, "success": False})
#
#         data = {"productSeq": "100020444"}
#         with self.client.post("/app/collection/exist.json", json=data, headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 2020, "success": False})
#
#         # 客户信息
#         with self.client.post("/app/customer/info.json", headers=my_headers, catch_response=True,timeout=60) as response:
#             my_assert(response, {"code": 2020, "success": False})
#
#     def on_start(self):
#         pass







